<template>
  <div class="uploadimg">
    <el-upload
      :action="actionUrl"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :on-exceed="handleExceed"
      :http-request="uploadFile"
      :limit="1"
      :disabled="disableIinput"
      list-type="picture-card"
    >
      <i class="el-icon-plus"/>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img :src="dialogImageUrl" width="100%" alt="">
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import * as video from '../../../api/video.js'
export default {
  /* eslint-disable vue/require-prop-types */
  props: ['videoId', 'status'],
  data() {
    return {
      disableIinput: false,
      actionUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      uploadDisabled: false,
      fileList: []
    }
  },
  beforeMount() {
    if (this.status !== 1) {
      this.getImg(this.videoId)
    }

    if (this.status === 3) {
      this.disableIinput = true
    }
  },
  created() {
    this.fileList = []
    this.initUploadFileurl()
  },
  methods: {
    initUploadFileurl() {
      this.actionUrl = process.env.BASE_API + 'file/uploadImageAndCrtThumbImagesRichEdit'
    },
    getImg(params) {
      video.getVideoById(params).then(res => {
        console.log('根据ID查询的视频信息：', res)
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        const obj = {}
        obj.url = res.data.data.records[0].img
        obj.id = res.data.data.records[0].id
        this.fileList.push(obj)
      })
    },
    uploadFile(content) {
      const file = content.file
      const isJPG =
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/gif'
      const isLt2M = file.size < 1048576
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 jpg、png、gif 格式!')
        this.fileList = []
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 1M!')
        this.fileList = []
        return false
      }
      const fd = new FormData()
      fd.append('images', content.file)
      fd.append('resPath', this.resPath)
      axios.post(content.action, fd).then(res => {
        if (res.data.code === 1) {
          this.$emit('videoSuccess', res.data.data[0].maxImageUrl)
          const obj = {}
          obj.url = res.data.data[0].minImageUrl
          this.fileList.push(obj)
          this.$message({
            message: '上传成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          })
        }
      })
    },
    incrementTotal() {
      this.$emit('emitSuccess', this.fileList)
    },
    handleRemove(file, fileList) {
      var _this = this
      this.fileList.forEach(function(item, index) {
        if (file.uid === item.uid) {
          _this.fileList.splice(index, 1)
        }
      })
    },
    handlePreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleExceed(files, fileList) {
      this.$message({
        message: '最多只能上传一张',
        type: 'warning'
      })
    }
  }
}
</script>

<style type="text/scss" lang="scss">
	.uploadimg{
		overflow: hidden;
		clear: both;
  }
</style>
